package com.cc.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class MyAdvice {
    @Pointcut("execution(* com.cc.mapper.*.*(..))")
    public void pt(){}

    //前置通知
    @Before("pt()")
    public void before(JoinPoint jp){

        System.out.println("调用时间"+System.currentTimeMillis());
        System.out.println("类名"+jp.getSignature().getDeclaringType().getName());
        Object[] args = jp.getArgs();
        for (Object arg : args) {
            System.out.println("参数"+arg);
        }
    }

    //后置通知
    @After("pt()")
    public void after(JoinPoint jp){

        System.out.println("调用时间"+System.currentTimeMillis());
    }
    //环绕通知
    @Around("execution(* com.cc.mapper.StudentMapper.addStudent(..))")
    public Object logAddStudent(ProceedingJoinPoint joinPoint) throws Throwable {
        // 前置逻辑
        System.out.println("前置通知");

        // 执行原方法并获取返回值
        Object result = joinPoint.proceed();

        // 后置逻辑
        System.out.println("后置通知");

        return result; // 必须返回原方法的结果
    }

    // 异常通知
    @AfterThrowing(value = "pt()" , throwing = "e")
    public void afterThrowing(JoinPoint jp, Exception e){
        System.out.println("异常通知");
        System.out.println("类名"+jp.getSignature().getDeclaringType().getName());
        System.out.println("方法名"+jp.getSignature().getName());
        System.out.println("异常信息"+ e.getMessage());
    }
}
